/*
 Given a string, find the length of the longest substring without repeating characters.

 Examples:

 Given "abcabcbb", the answer is "abc", which the length is 3.

 Given "bbbbb", the answer is "b", with the length of 1.

 Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
 */

//给你一个字符串，找到这个字符串中没有重复字母的最长子串长度。

let fs = require("fs");


/**
 * @param {string} s
 * @return {number}
 */
let lengthOfLongestSubstring = function (s) {
    let set = {};
    let maxStr = "";
    let str = "";
    for (let i = 0; i < s.length; i++) {
        let c = s.charAt(i);
        if (set[c] != null) {
            i = set[c];
            set = {};
            str = "";
            continue;
        }
        set[c] = i;
        str += c;
        //比较maxStr与str的长度，取最长
        maxStr = str.length > maxStr.length ? str : maxStr;
    }
    return maxStr.length;
};


console.log(lengthOfLongestSubstring("s"));
let longStr = fs.readFileSync("./longStr","utf8");
console.log(longStr);
console.log(lengthOfLongestSubstring(longStr));

//注:改方案在leetcode并没有通过longStr的测试，运行时间超过了3秒